//
//  Slidebar.swift
//  SwiftUIFly
//
//  Created by shen on 2023/10/18.
//

import SwiftUI

struct Slidebar: View {
    @Binding var selectedItem: DemoSection?

    var body: some View {
        List {
            OutlineGroup(datasources, children: \.items) { item in
                Label {
                    Text(item.title).font(.subheadline)
                } icon: {
                    Image(systemName: "folder").foregroundStyle(.tint)
                }
                .if(
                    item.items.isNilOrEmpty,
                    apply: { $0.labelStyle(.titleOnly) },
                    else: { $0.labelStyle(.titleAndIcon) }
                )
                .onTapGesture {
                    if item.items.isNilOrEmpty {
                        return
                    }
                    selectedItem = item
                }
            }
        }
        .listStyle(.sidebar)
    }

    var datasources: [DemoSection] {
        [
            DemoSection(title: "SwiftUI", items: [
                DemoSection(title: "Text", items: [DemoSection(title: "Text1", items: nil), DemoSection(title: "Text2", items: nil)]),
                DemoSection(title: "Button", items: nil),
            ]),
        ]
    }
}
